﻿using System.Collections.Generic;
using AutoMapper;
using B.S.Base.ErrorCode;
using B.S.Base.Infrastructrue;
using B.S.Base.Moain.Entities;
using B.S.Base.Read.Api.Application.Command.Products;
using B.S.Base.Read.Api.Dto;
using MediatR;

namespace B.S.Base.Read.Api.Application.CommandHander.Products
{
    public class ShowProductCommandHander : IRequestHandler<ShowProductCommand,APIPaging<ProductDTO>>
    {
        private readonly IBaseRepository<Product> pro;
        private readonly IMapper mapper;

        public ShowProductCommandHander(IBaseRepository<Product> pro, IMapper mapper)
        {
            this.pro = pro;
            this.mapper = mapper;
        }
        public Task<APIPaging<ProductDTO>> Handle(ShowProductCommand request, CancellationToken cancellationToken)
        {
            APIPaging<ProductDTO> result = new APIPaging<ProductDTO>();
            var user = pro.GetAll().Result;
            result.TotalCount = user.Count();
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.size);
            var model = user.OrderBy(x => x.ProductId).Skip((request.index - 1) * request.size).Take(request.size).ToList();
            var data = mapper.Map<List<ProductDTO>>(model);
            result.Code = APIEnum.操作成功;
            result.Msg = "获取成功";
            result.Items = data;
            return Task.FromResult(result);
        }

        

        

       
    }
}
